/****************************************************************************************
 * File      : app_usb.c
 *
 *
 ****************************************************************************************/
#include <rtthread.h>
#include "board.h"
#include "dfs_posix.h"

#include "usbh_usr.h"
#include "app_config.h"

extern struct rt_event usb_event;

void usb_disconnect_deal()
{
}


void usb_connect_deal()
{
}

static void usbapp_svr_thread_entry(void* parameter)
{
    rt_uint32_t ustate;
    rt_uint32_t level;
    while(1)
    {
        if(rt_event_recv(&usb_event,((DISCONNECT)|(CONNECT)),RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR,RT_WAITING_FOREVER,&ustate)==RT_EOK)
        {
            if(ustate==DISCONNECT)
            {
							usb_disconnect_deal();
            }
            else if(ustate==CONNECT)
            {
                rt_enter_critical();
                usb_connect_deal();
                rt_exit_critical();
            }
        }
    }
}



/**********************************************************************************************
******گ˽ĻԆúusbh_app_init

******٦ŜĨ˶úӢ˔ПԌԵʼۯ

******ˤɫӎ˽úϞ

******׵ ܘ ֵúRT_EOK

******עӢ˂ЮúϞ

******ʹԃ׶}ú
***********************************************************************************************/

//static struct rt_thread usbhost;
//static rt_uint8_t usbh_thread_stack[USBH_THREAD_STACK_SIZE];

//extern rt_device_t nor_flash_dev;
//extern void upgrade_info_rdwr_flash(rt_uint8_t rw_flag);
extern struct rt_event usb_event;

//rt_timer_t update_led_timer;
//extern void update_led_callback(void* parameter);
extern __ALIGN_BEGIN USB_OTG_CORE_HANDLE      USB_OTG_Core __ALIGN_END;
extern __ALIGN_BEGIN USBH_HOST                USB_Host __ALIGN_END;

static void usbh_svr_thread_entry(void* parameter)
{
    while(1)
    {
        USBH_Process(&USB_OTG_Core, &USB_Host);
        rt_thread_delay(1);
    }
}

int usbh_app_init(void)
{
    rt_err_t    result = RT_ERROR;

    int ret = RT_ERROR;

    rt_thread_t usbh;
    rt_thread_t usbapp;
    GPIO_InitTypeDef GPIO_InitStructure;
    /* USB disk enable pin */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init( GPIOC, &GPIO_InitStructure );
    GPIO_ResetBits( GPIOC, GPIO_Pin_9);

    rt_event_init(&usb_event,"usb_event",RT_IPC_FLAG_FIFO);

    usbh = rt_thread_create("usbh_svr",
                            usbh_svr_thread_entry,
                            RT_NULL,
                            USBH_THREAD_STACK_SIZE,
                            USBH_THREAD_PRIORITY,
                            USBH_THREAD_TIME_TICK);


    rt_thread_startup(usbh);


    ret = RT_ERROR;
    usbapp = rt_thread_create( "usbapp_svr",
                               usbapp_svr_thread_entry,
                               RT_NULL,
                               USBAPP_THREAD_STACK_SIZE,
                               USBAPP_THREAD_PRIORITY,
                               USBAPP_THREAD_TIME_TICK);
    rt_thread_startup(usbapp);

    //ղߪNor flash ʨѸ
//		nor_flash_dev = rt_device_find(NOR_FLASH);
//		if (rt_device_open(nor_flash_dev, RT_DEVICE_OFLAG_RDWR) == RT_EOK)
//		{
//			rt_kprintf("NOR FLASH OPEN SUCCESS\n");
//			// ׁȡNor flash ʽܶӎ˽
//			upgrade_info_rdwr_flash(UP_READ);
//		}
//		else
//		{
//			rt_kprintf("NOR FLASH OPEN FAILURE\n");
//		}

    return ret;
}